package com.atguigu.order.service.impl;

import com.atguigu.clients.ProductClient;
import com.atguigu.order.mapper.OrderMapper;
import com.atguigu.order.service.OrderService;
import com.atguigu.param.OrderParam;
import com.atguigu.param.ProductCollectParam;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.Product;
import com.atguigu.to.OrderToProduct;
import com.atguigu.utils.R;
import com.atguigu.vo.CartVo;
import com.atguigu.vo.OrderVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName:OrderServiceImpl
 * Package:com.atguigu.order.service.impl
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/22 22:00
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ProductClient productClient;

    @Override
    @Transactional
    public R save(OrderParam orderParam) {
        ArrayList<Integer> cartIds = new ArrayList<>();
        ArrayList<OrderToProduct> orderToProducts = new ArrayList<>();
        ArrayList<Order> orders = new ArrayList<>();

        Integer userId = orderParam.getUserId();
        long orderId = System.currentTimeMillis();

        for (CartVo cartVo : orderParam.getProducts()) {
            cartIds.add(cartVo.getId());
            OrderToProduct orderToProduct = new OrderToProduct();
            orderToProduct.setNum(cartVo.getNum());
            orderToProduct.setProductId(cartVo.getProductID());
            orderToProducts.add(orderToProduct);

            Order order = new Order();
            order.setOrderId(orderId);
            order.setOrderTime(orderId);
            order.setUserId(userId);
            order.setProductId(cartVo.getProductID());
            order.setProductNum(cartVo.getNum());
            order.setProductPrice(cartVo.getPrice());
            orders.add(order);

        }
        saveBatch(orders);

        rabbitTemplate.convertAndSend("topic.ex", "clear.cart", cartIds);
        rabbitTemplate.convertAndSend("topic.ex", "sub.number", orderToProducts);


        return R.ok("保存成功");
    }

    @Override
    public R list(Integer userId) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("user_id", userId);
        List<Order> list = list(orderQueryWrapper);

        Map<Long, List<Order>> orderMap = list.stream().collect(Collectors.groupingBy(Order::getOrderId));
        List<Integer> productIds = list.stream().map((Order::getProductId)).collect(Collectors.toList());


        ProductCollectParam productCollectParam = new ProductCollectParam();
        productCollectParam.setProductIds(productIds);
        List<Product> productList = productClient.cartList(productCollectParam);
        Map<Integer, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getProductId, v -> v));


        List<List<OrderVo>> result = new ArrayList<>();

        for (List<Order> orders : orderMap.values()){
            List<OrderVo> orderVos = new ArrayList<>();
            for (Order order: orders){
                OrderVo orderVo = new OrderVo();
                BeanUtils.copyProperties(order,orderVo);
                Product product = productMap.get(order.getProductId());
                orderVo.setProductName(product.getProductName());
                orderVo.setProductPicture(product.getProductPicture());
                orderVos.add(orderVo);
            }
            result.add(orderVos);
        }
        R r = R.ok("订单数据获取成功", result);
        log.info("OrderServiceImpl.list业务完成,结果是:{}",result);
        return r;
    }
}
